﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ElectronicJournal
{
    //  public delegate void NewAuthorAddedDelegate(Author author);

    public class EJournalRepository
    {
        //  public event NewAuthorAddedDelegate NewAuthorAddedDelegateEvent;

        //public void InvokeNewAuthorAddedDelegateEvent()
        //{
        //    NewAuthorAddedDelegate handler = NewAuthorAddedDelegateEvent;
        //    if (handler != null) handler(this);
        //}

        private EJournalDBClassesDataContext _eJDataContext;
        protected EJournalRepository()
        {
            _eJDataContext = new EJournalDBClassesDataContext();
        }

        private sealed class RepositoryCreator
        {
            private static readonly EJournalRepository instance = new EJournalRepository();
            public static EJournalRepository Instance { get { return instance; } }
        }

        public static EJournalRepository Instance
        {
            get { return RepositoryCreator.Instance; }
        }

        public void AddAuthor(String authorName,
            string authorSurname,
            string thirdName,
            string authorEmail,
            bool receivesPubState,
            bool receivesNews,
            string authorPhoneNumber,
            Guid authorUserId,
            Guid scientificDir
            )
        {
            var newAuthor = new Author();
            newAuthor.AuthorFirstName = authorName;
            newAuthor.AuthorSecondName = authorSurname;
            newAuthor.AuthorThirdName = thirdName;
            newAuthor.AuthorUserId = authorUserId;
            newAuthor.AuthorEmail = authorEmail;
            newAuthor.AuthorReceivesMagazineNews = receivesNews;
            newAuthor.AuthorReceivesPublicationState = receivesPubState;
            newAuthor.AuthorPhoneNumber = authorPhoneNumber;
            newAuthor.AuthorScientificDirectionId = scientificDir;
            newAuthor.AuthorId = Guid.NewGuid();
            _eJDataContext.Authors.InsertOnSubmit(newAuthor);
            _eJDataContext.SubmitChanges();
        }

        public void AddReviewer(String name,
          string surname,
          string thirdName,
          string email,
          string number,
          Boolean receivesByEmail,
          Boolean receivesBySms,
          Guid UserId)
        {
            var reviewer = new Reviewer()
            {
                ReviewerUserId = UserId,
                ReviewerEmail = email,
                ReviewerFirstName = name,
                ReviewerLastName = surname,
                ReviewerThirdName = thirdName,
                ReviewerReceivesByMail = receivesByEmail,
                ReviewersReceivesBySms = receivesBySms,
                ReviewerPhoneNumber = number
            };

            _eJDataContext.Reviewers.InsertOnSubmit(reviewer);
            _eJDataContext.SubmitChanges();
        }

        public List<ScientificDirection> AllScientificFirection()
        {
            return _eJDataContext.ScientificDirections.ToList();
        }

        public bool IsSubmitRegistration(Guid userGuid) // проверка подтверждения регистрации
        {
            if (_eJDataContext.Authors.ToList().Where(n => n.AuthorUserId == userGuid).Count() != 0 ||
                _eJDataContext.Reviewers.ToList().Where(n => n.ReviewerUserId == userGuid).Count() != 0)
            {
                return true;
            }
            return false;
        }

        public List<Publication> GetAllPublicationsForAuthor(Guid userId)
        {
            Guid authorId = _eJDataContext.Authors.SingleOrDefault(n => n.AuthorUserId == userId).AuthorId;
            List<Publication> PubList = new List<Publication>();
            _eJDataContext.AuthorToPubs.ToList().ForEach(autToPub =>
                                                             {
                                                                 if (autToPub.AuthorId == authorId)
                                                                     PubList.Add(
                                                                         _eJDataContext.Publications.Single(
                                                                             pub => pub.Id == autToPub.PubId));
                                                             });
            return PubList;

        }
    }
}